package com.aj.controller;

import com.aj.Interface.RoleService;
import com.aj.Interface.SetmealService;
import com.aj.common.MessageConstant;
import com.aj.pojo.CheckItem;
import com.aj.pojo.Role;
import com.aj.common.RedisConstant;
import com.aj.pojo.Setmeal;
import com.aj.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;



    //²éÑ¯ËùÓÐÓÃ»§
    @RequestMapping("/findall")
    public Result findall() {
          List<Role> rolelist= roleService.findall();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,rolelist);
    }

  //分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = roleService.findpage(queryPageBean);
        return pageResult;
    }

    //添加角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findRoleByid")
    public Result findRoleByid(Integer id) {
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSIONIDS_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIONIDS_FAIL);
        }
    }

    @RequestMapping("/findMenuIdsBySetmealid")
    public Result findMenuIdsBySetmealid(Integer id) {
        try {
            List<Integer> menuIds = roleService.findMenuIdsBySetmealid(id);
            return new Result(true, MessageConstant.QUERY_MENUIDS_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENUIDS_FAIL);
        }
    }

    @RequestMapping("/editTrue")
    public Result editTrue(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.editTrue(role,permissionIds,menuIds);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }
}
